﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectManagement.Domain.Services;
using ProjectManagement.Domain.Entities;
using System.Web.Security;
using ProjectManagement.Models;
using ProjectManagement.Web.Models;

namespace ProjectManagement.Web.Controllers
{
    public class CompanyController : Controller
    {
        //
        // GET: /Company/
		private static CompanyService _companyService;
		private static UserService _userService;

		public CompanyController(CompanyService companySer,UserService userSer)
		{
			_companyService = companySer;
			_userService = userSer;
		}

        public ActionResult CompanyList()
        {
            return View();
        }

		public JsonResult Companylists()
		{
			int page = int.Parse(Request.Form["page"]);
			int rp = int.Parse(Request.Form["rp"]);
			string qtype = Request.Form["qtype"].ToString();
			string query = Request.Form["query"].ToString();
			string sortname = Request.Form["sortname"].ToString();
			string sortorder = Request.Form["sortorder"].ToString();

			var companies = _companyService.QueryAll().AsQueryable<Company>();

			if (!string.IsNullOrEmpty(qtype) && !string.IsNullOrEmpty(query))
			{
				companies =companies.Like(qtype, query);
			}

			companies = companies.Skip((page - 1) * rp).Take(rp);

			if (!string.IsNullOrEmpty(sortname) && !string.IsNullOrEmpty(sortorder))
			{
				companies = companies.OrderBy(sortname, (sortorder == "asc"));
			}

			FlexigridObject flexigridObject = new FlexigridObject();
			flexigridObject.page = page;
			flexigridObject.total = companies.Count();
			foreach (Company company in companies)
			{
				FlexigridRow cell = new FlexigridRow()
				{
					id = company.ID.ToString(),
					cell = FlexigridListHelper.GetPropertyList(company)
				};
				flexigridObject.rows.Add(cell);
			}


			return Json(flexigridObject, JsonRequestBehavior.AllowGet);
		}


		[HttpPost]
		public ActionResult AddCompany(Company company)
		{
			if (ModelState.IsValid)
			{
				FormsIdentity id = (FormsIdentity)HttpContext.User.Identity;
				FormsAuthenticationTicket ticket = id.Ticket;
				int userId = int.Parse(ticket.UserData);
				var currentUser = _userService.Get(userId);
				company.Creator = currentUser;
				company.CreateTime = DateTime.Now;
				company.EditTime = DateTime.Now;
				company.LastContactTime = DateTime.Now;
				_companyService.Save(company);
				TempData["Message"] = "保存成功！";
				return RedirectToAction("CompanyList");
			}
			else
			{
				TempData["Message"] = "保存失败！";
				return RedirectToAction("CompanyList");
			}
		}

		public ActionResult EditCompany(string id)
		{
			var company = _companyService.Get(int.Parse(id));
			return View(company);
		}
		[HttpPost]
		public ActionResult EditCompany(Company company)
		{
			if (ModelState.IsValid)
			{
				_companyService.Save(company);
				return RedirectToAction("EditCompany");
			}
			else
			{
				return View(company);
			}
		}

		[HttpDelete]
		public ActionResult DeleteComs(string ids)
		{
			var nid = ids.Split(new char[] { ','});
			foreach (var id in nid)
			{
				_companyService.DeleteCompanyById(int.Parse(id));
			}
			return RedirectToAction("CompanyList");
		}

		//前台ajax请求,返回某省下面的市区
		[HttpPost]
		public JsonResult GetSelectedList(string id)
		{
			int sid = int.Parse(id);
			List<Object> list = new List<Object>();
			var cities =(new CityService()).QueryCityByProId(sid);
			foreach (var item in cities)
			{
				list.Add(new
				{
					ID = item.ID,
					Name = item.Name
				});
			}
			return Json(list, JsonRequestBehavior.AllowGet);
		}

    }
}
